<?php
/**
 * User:chendenan
 * Created On 2022/3/16 0016 21:21
 */
namespace app\common\business;
use app\common\model\mysql\Category as CategoryModel;
class Category{
    public $model = null;
    public function __construct()
    {
        $this->model = new CategoryModel();
    }

    /**
     * 添加分类
     * @param $data
     * @return false|mixed
     * @throws \think\Exception
     */
    public function add($data){
        if(empty($data)){
            return false;
        }
        $data['status'] = config("status.mysql.table_normal");
        $name = $data['name'];
        if((new CategoryModel())->getCategoryByName($name)!=null){
            throw new \think\Exception("分类已存在");
        }
        try {
            $this->model->save($data);
        }catch (\Exception $e){
            throw new \think\Exception("服务内部异常");
        }
        return $this->model->id();
    }

    /**
     * 查询所有分类
     * @return CategoryModel[]|array|\think\Collection|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getCategorys(){
        $field = ["id,name,pid"];
        $categorys = $this->model->getCategorys($field);
        if(!$categorys){
            return $categorys;
        }
        $categorys = $categorys->toArray();
        return $categorys;
    }

    /**
     * 查询分类数据 默认显示10个
     * @param $data
     * @param $num
     * @return array
     */
    public function getLists($data,$num){
        $list = $this->model->getLists($data,$num);
        if(!$list){
            return [];
        }
        $result = $list->toArray();
        $result['render'] = $list->render();
        return $result;
    }

    /**
     * 根据ID查询记录
     * @param $id
     * @return array|bool
     */
    public function getById($id){
        $result = $this->model->find($id);
        if(empty($result)){
            return [];
        }
        $result = $result->toArray();
        return $result;
    }
    public function updateById($id,$data){
        //查询数据是否存在
        $res = $this->getById($id);
        if(!$res){
            throw new \think\Exception("记录不存在");
        }
        try{
            $res = $this->model->updateById($id,$data);
        }catch (\Exception $e){
            trace('分类修改保存失败','error');
            return false;
        }
        return $res;
    }

    /**
     * 获取一级分类内容
     * @param $pid
     * @param $field
     * @return array
     */
    public function getByPid($pid=0,$field="id,name,pid"){
        try{
            $res = $this->model->getByPid($pid,$field);
        }catch (\Exception $e){
            trace("分类内部异常","error");
            return [];
        }
        $res = $res->toArray();
        return $res;
    }

    /**
     * 获取栏目及子栏目信息
     * @param $categoryId
     * @return array
     */
    public function getCategoryById($categoryId){
        $field = "id as category_id,name,icon";
        try{
            $result = $this->model->getCategoryById($categoryId,$field);
        }catch(\Exception $e){
            trace($e->getMessage(),"error");
            return [];
        }
        return $result;
    }

}